package Java8.map;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class computeIfBasentTest {

  public static void main(String[] args) throws FileNotFoundException {

      Map<String, Integer> map = new HashMap();
      map.put("1", 8);
      map.put("2", 12);
      map.put("3", 53);
      map.put("4", 33);
      map.put("5", 11);
      map.put("6", 3);
      map.put("7", 3);
      List<Entry<String, Integer>> list = new ArrayList(map.entrySet());
      Collections.sort(list, (o1, o2) -> (o1.getValue() - o2.getValue()));
      System.out.println(list.get(0));
      Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue()));
      System.out.println(list.get(0));

      File dictionay = new File("/Users/dingjianfeng/test.lua");

      int minGroupSize = Integer.parseInt("10");

      Map<String, Set<String>> groups = new HashMap<>();

      try (Scanner s = new Scanner(dictionay)) {
          while (s.hasNext()) {
              String word = s.next();
        System.out.println(word);
        System.out.println("groups.size()====>" + groups.size());
        System.out.println("===>" + groups.computeIfAbsent(alphabetize(word), (unused) -> new TreeSet<>()).add(word));
        System.out.println("groups.size()<====" + groups.size());
      }
    }

  }

  private static String alphabetize(String s) {
    char[] a = s.toCharArray();
    Arrays.sort(a);
    return new String(a);
  }

}
